<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use \Common\Model\Cate;
use \Common\Model\Goods;
use \Common\Model\GoodsAttr;
use \Common\Model\Brand;
// use \Common\Model\TypeAttr;
//列表控制器
class ListController extends Controller{

	//构造函数
	public function __init()
	{
	}
    //显示列表耶
    /**
     *
     */
    public function index(){
        //1.获取顶级分类数据
        $cate=new Cate;
        $topCate=$cate->where("pid=0")->get();
        View::with('topCate',$topCate);
        //2.通过当前分类的cid获得所有子集的cid
        $cid=Q('get.cid',0,'intval');
        $goodsAttr=new GoodsAttr;
        $brand=new Brand;$goods=new Goods;
        $cids=$cate->getSon($cate->get(),$cid);
        //获得顶部搜索对应的父集
        $pids=$cate->getFather($cate->get(),$cid);
        if($pids){
            $pidData=$cate->where("cid IN(".implode(',',$pids).")")->get();
        }else{
            $pidData=[];
        }
        //分配对应的父集
        View::with('pidData',$pidData);
        $cids[]=$cid;
        //查找当前分类下所对应的子分类的名称
        //3.根据cid获得商品的gid
        $bid=isset($_GET['bid'])? $_GET['bid']:0;
        if($bid){
            $gids=$goods->where("brand_bid={$bid} AND category_cid IN(".implode(',',$cids).")")->lists('gid');
        }else{
            $gids=$goods->where("category_cid IN(".implode(',',$cids).")")->lists('gid');
        }
        if($cid==0){
             $cateData=$cate->where("pid=0")->get();
             $brandData=$brand->lists('bname');
             $newAttr=[];
            $goodsData = $gids ? $goods->where("gid IN(" . implode(',', $gids) . ")")->get() : [];
        }else{
            if($gids){
            $cateData=$cate->where("pid={$cid}")->get();
            //获取对应的品牌
            $brandData=$goods->join('brand','brand_bid','=','bid')->where("gid IN(".implode(',',$gids).")")->groupBy('bname')->lists('bid,bname');
//                P($brandData);
            //4.通过gid查询goodsattr的商品属性
                    $gaData=$goodsAttr->where("goods_gid IN(".implode(',',$gids).")")->groupBy('gavalue')->get();
                    // P($gaData);
                    //5.把有相同taid的属性放到一起
                    $temp=[];
                    foreach($gaData as $v){
                        $temp[$v['type_attr_taid']][]=$v;
                    }
                    // P($temp);
                    //组合名字和键值
                    // $typeAttr= new TypeAttr;
                    $newAttr=[];
                    foreach($temp as $taid=>$v){
                        $newAttr[]=[
                        'name'=>$goodsAttr->join('typeattr','type_attr_taid','=','taid')->where("gaid={$v[0]['gaid']}")->groupBy('taname')->pluck('taname'),
                        'value'=>$v
                        ];
                    }
                    //筛选当前分类所对应的所有商品**********************
                    //1.地址栏0_0_0_0_style="background: url({{$goodsData['listpic']}}) no-repeat; background-size:240px 144px;background-position:center right;0_0效果
                    $num=count($newAttr);
                    $param=isset($_GET['param'])? explode('_',$_GET['param']):array_fill(0,$num,0);
                    // P($param);P($newAttr);
                    View::with('param',$param);
                    //2.进行筛选操作，获得最终的商品id
                    $goodsGids=$this->filter($param,$gids);
                    if($goodsGids){
                        $goodsData=$goods->where("gid IN(".implode(',',$goodsGids).")")->get();
                    }else{
                        $goodsData=[];
                    }
                }else{
                    $newAttr=[];
                    $brandData=[];
                    $cateData=[];
                    $goodsData=[];
                }
        }
        View::with('goodsData',$goodsData);
        View::with('cateData',$cateData);
        View::with('brandData',$brandData);
        View::with('newAttr',$newAttr);
        View::make();
    }
    //筛选商品的自定义方法
    private function filter($param,$gids){
        $goodsAttr=new GoodsAttr;
        // P($param);
        $allGids=[];
        foreach($param as $gaid){
            //如果是非不限选项，根据gaid获得商品的gid
            if($gaid){
                //查询到当前商品属性对应的名称
                //**//**同一个gavalue可能对应多个属性值，之前压缩过，完整的gid需要通过gavalue查找**//**//
                $gavalue=$goodsAttr->where("gaid={$gaid}")->pluck('gavalue');
                //查找到gavalue对应的gid
                $allGids[]=$goodsAttr->where("gavalue='{$gavalue}'")->lists('goods_gid');
            }
        }
        // P($allGids);
        //$allGids有值，即用户选择具体属性值的时候，非不限条件
        if($allGids){
            $gidFill=$allGids[0];
            foreach($allGids as $v){
                //获取所有数组的交集，每两组一次进行求交集
                $gidFill=array_intersect($v,$gidFill);
            }
            //$gid可能有其他分类的gid，为了确保是当前分类的，需要再次同之前获取到当前分类的所有商品id比较，求交集
            $finalGids=array_intersect($gids,$gidFill);
        }else{
            //如果用户没有选择任何属性的时候，即不限条件
            $finalGids=$gids;
        }
        return $finalGids;
    }
}